package com.javaclimb.accounting.controller;

import com.javaclimb.accounting.po.Account;
import com.javaclimb.accounting.po.Transfer;
import com.javaclimb.accounting.po.User;
import com.javaclimb.accounting.service.TradeService;
import com.javaclimb.accounting.service.TransferService;
import com.javaclimb.accounting.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/accounting")
public class TransferController {

    @Autowired
    TransferService transferService;
    @Autowired
    private TradeService tradeService;

    /**
     * 转账
     */
    @RequestMapping(value = "/transfer/name",method = RequestMethod.POST)
    public Object transferName (String beforeName, String afterName, Double money,User user) {
        if (money.equals(0)) {
            return CommonResult.failed("转账失败");
        }
        if (beforeName.equals(afterName)){
            return CommonResult.failed("转账失败,不能转账给自己账户");
        }
        //查询用户
        List<User> users = transferService.getUsersByName(beforeName, afterName);
        User before, after;
        if (users.size() != 2) {
            return CommonResult.failed("转账失败,该收款人不存在");
        }
        if (users.get(0).getUserName().equals(beforeName)) {
            before = users.get(0);
            after = users.get(1);
        } else {
            before = users.get(1);
            after = users.get(0);
        }
        //先验证是否设置支付密码
        boolean i = tradeService.getUserPay(user.getUserName());
        if (i == true) {
            //在验证支付密码是否正确
            boolean a = tradeService.verifyUserPay(user.getUserName(), user.getUserPay());
            if (a == true) {
                //查询电子账户
                List<Account> accounts = transferService.getAccount(before.getUserAccId(), after.getUserAccId());
                Account beforeAccount, afterAccount;
                if (accounts.size() != 2) {
                    return CommonResult.failed("转账失败");
                }
                if (accounts.get(0).getAccId().equals(before.getUserAccId())) {
                    beforeAccount = accounts.get(0);
                    afterAccount = accounts.get(1);
                } else {
                    beforeAccount = accounts.get(1);
                    afterAccount = accounts.get(0);
                }
                //判断
                BigDecimal moneys = new BigDecimal(money);
                if (beforeAccount.getAccTemp().compareTo(moneys) < 0) {
                    return CommonResult.failed("转账失败,余额不足");
                }
                //电子账户余额变少 变多
                //转账表记录增加
                BigDecimal befores = new BigDecimal(beforeAccount.getAccTemp().doubleValue() - money);
                BigDecimal afters = new BigDecimal(afterAccount.getAccTemp().doubleValue() + money);

                Transfer transfer = new Transfer(before.getUserId(), after.getUserId(), moneys, after.getUserName(), before.getUserName(), befores, afters);
                transferService.transfer(transfer, beforeAccount, afterAccount, money);


                return CommonResult.success(transfer,"转账成功");
            }
            return CommonResult.failed("支付密码错误");
        }
        return CommonResult.failed("用户未设置支付密码,请先设置支付密码");
    }

    @RequestMapping(value = "/transfer/tel",method = RequestMethod.POST)
    public Object transferTel (String beforeTel,String afterTel, Double money,User user){
        if(money.equals(0.0)){
            return CommonResult.failed("转账失败");
        }
        if (beforeTel.equals(afterTel)){
            return CommonResult.failed("转账失败,不能转账给自己账户");
        }
        //查询用户
        List<User> users = transferService.getUsersByTel(beforeTel, afterTel);
        User before, after;
        if(users.size() != 2){
            return CommonResult.failed("转账失败,该收款人不存在");
        }
        if(users.get(0).getUserTel().equals(beforeTel)){
            before = users.get(0);
            after = users.get(1);
        }else{
            before = users.get(1);
            after = users.get(0);
        }
        //先验证是否设置支付密码
        boolean i = tradeService.getUserPay(user.getUserName());
        if (i == true) {
            //在验证支付密码是否正确
            boolean a = tradeService.verifyUserPay(user.getUserName(), user.getUserPay());
            if (a == true) {
        //查询电子账户
        List<Account> accounts = transferService.getAccount(before.getUserAccId(), after.getUserAccId());
        Account beforeAccount, afterAccount;
        if(accounts.size() != 2){
            return CommonResult.failed("转账失败");
        }
        if(accounts.get(0).getAccId().equals(before.getUserAccId())){
            beforeAccount = accounts.get(0);
            afterAccount = accounts.get(1);
        }else{
            beforeAccount = accounts.get(1);
            afterAccount = accounts.get(0);
        }
        //判断
        BigDecimal moneys = new BigDecimal(money);
        if(beforeAccount.getAccTemp().compareTo(moneys) < 0){
            return CommonResult.failed("转账失败,余额不足");
        }
        //电子账户余额变少 变多
        //转账表记录增加
        BigDecimal befores = new BigDecimal(beforeAccount.getAccTemp().doubleValue() - money);
        BigDecimal afters = new BigDecimal(afterAccount.getAccTemp().doubleValue() + money);

        Transfer transfer = new Transfer(before.getUserId(),after.getUserId(),moneys,after.getUserName(),before.getUserName(),befores, afters);
        transferService.transfer(transfer, beforeAccount, afterAccount, money);


        return CommonResult.success(transfer,"转账成功");
    }
            return CommonResult.failed("支付密码错误");
        }
        return CommonResult.failed("用户未设置支付密码,请先设置支付密码");
    }


}
